
#include "lcd_init.h"
#include "stdio.h"

void lcd_init()
{
	uint16_t a;
	uint16_t b;
	uint16_t c;
	uint16_t d;
	
	//
	LCD_CmdWrite(0x04);
	//HAL_Delay(1);
	a = LCD_DataRead();
	b = LCD_DataRead();
	c = LCD_DataRead();
	d = LCD_DataRead();
	printf("ID:%02x,%02x,%02x,%02x \r\n",a,b,c,d);

	HAL_Delay(10);
	////////lcd init//////
	LCD_CmdWrite(0x11); //Exit Sleep
	HAL_Delay(120);

	LCD_CmdWrite(0xDF);  
	LCD_DataWrite(0x5A); 
	LCD_DataWrite(0x69); 
	LCD_DataWrite(0X02); 
	LCD_DataWrite(0X01);
	
	LCD_CmdWrite(0x36);    // Memory Access Control ST7789
	LCD_DataWrite(0x00); //
	LCD_CmdWrite(0x3A);   
	LCD_DataWrite(0x05); 
	
	LCD_CmdWrite(0x21); 
	
	LCD_CmdWrite(0xB2);  
	LCD_DataWrite(0x0C); 
	LCD_DataWrite(0x0C); 
	LCD_DataWrite(0X00); 
	LCD_DataWrite(0X33);
	LCD_DataWrite(0X33); 
	LCD_CmdWrite(0xB7);   
	LCD_DataWrite(0x75);   

	LCD_CmdWrite(0xBB);    // 
	LCD_DataWrite(0x23); 
	LCD_CmdWrite(0xC0);    // 
	LCD_DataWrite(0x2C); 
	LCD_CmdWrite(0xC2);    //
	LCD_DataWrite(0x01); 
	LCD_CmdWrite(0xC3);  
	LCD_DataWrite(0x0B); 
	LCD_CmdWrite(0xC4);  
	LCD_DataWrite(0x20); 
	LCD_CmdWrite(0xC6);  
	LCD_DataWrite(0x0F); 
	LCD_CmdWrite(0xD0);  
	LCD_DataWrite(0xA4); 
	LCD_DataWrite(0xA1); 

	
	LCD_CmdWrite(0xE0);    //Set Gamma 
	LCD_DataWrite(0xD0); 
	LCD_DataWrite(0x01); 
	LCD_DataWrite(0x03); 
	LCD_DataWrite(0x09); 
	LCD_DataWrite(0x08); 
	LCD_DataWrite(0x07); 
	LCD_DataWrite(0x2B); 
	LCD_DataWrite(0X13); 
	LCD_DataWrite(0x40); 
	LCD_DataWrite(0x0F); 
	LCD_DataWrite(0x1A); 
	LCD_DataWrite(0x18); 
	LCD_DataWrite(0x19); 
	LCD_DataWrite(0x1D); 

	LCD_CmdWrite(0XE1);    //Set Gamma 
	LCD_DataWrite(0xD0); 
	LCD_DataWrite(0x01); 
	LCD_DataWrite(0x03); 
	LCD_DataWrite(0x08); 
	LCD_DataWrite(0x08); 
	LCD_DataWrite(0x06); 
	LCD_DataWrite(0x2A); 
	LCD_DataWrite(0X43); 
	LCD_DataWrite(0x44); 
	LCD_DataWrite(0x0F); 
	LCD_DataWrite(0x1A); 
	LCD_DataWrite(0x19); 
	LCD_DataWrite(0x18); 
	LCD_DataWrite(0x1D); 		

	LCD_CmdWrite(0x29); //display on
	//////////////////////
}

void lcd_SetColor(uint32_t color)
{
	uint32_t x = 0; uint32_t y = 0;
	for( y=0; y < LCD_Height; y++ )
		{for( x=0; x < LCD_Width; x++ )
		{
		
			//LCD_DataWrite( color );
			lcddrv_FillColorPre(x,y,color);
			//lcddrv_FillColorPre(x,y,LCD_Width,LCD_Height,color);
		}
	}
}

void lcddrv_FillColorPre(uint16_t _usX, uint16_t _usY, uint16_t color)
{
    //显示区域限制
    LCD_CmdWrite(ST7789_CMD_CASETF);
    LCD_DataWrite(_usX >> 8);
    LCD_DataWrite(_usX);
    LCD_DataWrite(_usX >> 8); // LCD_DataWrite( (_usX + _usWidth - 1) >> 8);
    LCD_DataWrite(_usX);
 
    LCD_CmdWrite(ST7789_CMD_RASETF );
    LCD_DataWrite(_usY >> 8);
    LCD_DataWrite(_usY);
    LCD_DataWrite(_usY >> 8 );
    LCD_DataWrite(_usY);
    //准备接收数据
    LCD_CmdWrite(ST7789_CMD_RAMWR);

	LCD_DataWrite( color );
}

/*void lcddrv_FillColorPre(uint16_t _usX, uint16_t _usY, uint16_t _usWidth , uint16_t _usHeight, uint16_t color)
{
    //显示区域限制
    LCD_CmdWrite(ST7789_CMD_CASETF);
    LCD_DataWrite(_usX >> 8);
    LCD_DataWrite(_usX );
    LCD_DataWrite( (_usX + _usWidth - 1) >> 8);
    LCD_DataWrite( (_usX + _usWidth - 1) );
 
    LCD_CmdWrite(ST7789_CMD_RASETF );
    LCD_DataWrite(_usY >> 8);
    LCD_DataWrite(_usY );
    LCD_DataWrite( (_usY + _usHeight - 1) >> 8 );
    LCD_DataWrite( (_usY + _usHeight - 1) );
    //准备接收数据
    LCD_CmdWrite(ST7789_CMD_RAMWR);
	LCD_DataWrite( color );
}*/